<?php


namespace App\Libs\Contracts\Push\Jpush;

use App\Models\User;
use JPush\Client;

require 'autoload.php';

class JpushServer
{
    private $app_key = "9a5f7c7c9f85da2dd93bd32c";

    private $master_secret = "6db74088393498ec6011a7c0";

    private $app_teacher_key = "36654f09f5aa61a27f459c75";

    private $master_teacher_secret = "07515a2eb6b63c3fa11f4ced";

    public function push($title, $content, $userId)
    {

        $extras = "";
        if ($userId) {
            $user = User::find($userId);

            if ($user['pushType'] == "JPUSH") {
                if ($user['type'] == "ANDROID") {
                    \Log::info("极光推送");
                    $client = new Client($this->app_key, $this->master_secret);
                    $pusher = $client->push();
                    try {

                        $status = $pusher->setPlatform(["android"])
                            ->addRegistrationId([$user['registrationId']])
                            ->setNotificationAlert($title)
                            ->androidNotification($content, array(
                                'title' => $title,
                                'extras' => $extras
                            ))
                            ->message($content, array(
                                'title' => $title,
                                'content_type' => 'text',
                                'extras' => $extras
                            ))
                            ->options(array(
                                //                    'apns_production' => false // true 生产环境 false 开发环境
                                'production' => false // true 生产环境 false 开发环境
                            ))
                            ->setOptions(100000, 0, null, true)
                            ->send();
                        \Log::info($status);
                        return true;
                    } catch (\JPush\Exceptions\JPushException $e) {
                        \Log::error($e);
                        return false;
                    }
                }
                if ($user['type'] == "IOS") {
                    \Log::info(":USERID=".$userId);
                    \Log::info("极光推送");
                    $client = new Client($this->app_key, $this->master_secret);
                    $res = $client->push()
                        ->setPlatform(array('ios'))
                        ->addRegistrationId([$user['registrationId']])
                        ->setNotificationAlert($title)
                        ->iosNotification($content, array(
                            'sound' => 'default',
                            'extras' => $extras
                        ))
                        ->message($content, array(
                            'title' => $title,
                            'content_type' => 'text',
                            'extras' => $extras,
                        ))
                        ->options(array(
                            //                    'apns_production' => false // true 生产环境 false 开发环境
                            'production' => false // true 生产环境 false 开发环境
                        ))
                        ->setOptions(100000, 0, null, true)
                        ->send();
                    \Log::info($res);

                    return true;
                }
            }

        } else {
            \Log::info("极光推送");
            $client = new Client($this->app_key, $this->master_secret);
            $pusher = $client->push();
            $res = $pusher->setPlatform(["android", "ios"])
                ->addAllAudience()
                ->setNotificationAlert($title)
                ->androidNotification($content, array(
                    'title' => $title,
                    'extras' => $extras
                ))
                ->iosNotification($content, array(
                    'sound' => 'default',
                    'extras' => $extras
                ))
                ->message($content, array(
                    'title' => $title,
                    'content_type' => 'text',
                    'extras' => $extras
                ))
                ->options(array(
                    //                    'apns_production' => false // true 生产环境 false 开发环境
                    'production' => false // true 生产环境 false 开发环境
                ))
                ->setOptions(100000, 0, null, true)
                ->send();
            \Log::info($res);
            return true;
        }


    }

    public function pushOne($title, $content, $userId,$array,$appType)
    {

        $extras = $array;
        if ($appType == "USER"){
            $client = new Client($this->app_key, $this->master_secret);

        }else{
            $client = new Client($this->app_teacher_key, $this->master_teacher_secret);

        }
        if ($userId) {
            $user = User::find($userId);

            if ($user['pushType'] == "JPUSH") {
                if ($user['type'] == "ANDROID") {
                    \Log::info("极光推送");

                    $pusher = $client->push();
                    try {

                        $status = $pusher->setPlatform(["android"])
                            ->addRegistrationId([$user['registrationId']])
                            ->setNotificationAlert($title)
                            ->androidNotification($content, array(
                                'title' => $title,
                                'extras' => $extras
                            ))
                            ->message($content, array(
                                'title' => $title,
                                'content_type' => 'text',
                                'extras' => $extras
                            ))
                            ->options(array(
                                //                    'apns_production' => false // true 生产环境 false 开发环境
                                'production' => true // true 生产环境 false 开发环境
                            ))
                            ->setOptions(100000, 86400, null, true)
                            ->send();
                        \Log::info($status);
                        return true;
                    } catch (\JPush\Exceptions\JPushException $e) {
                        \Log::error($e);
                        return false;
                    }
                }
                if ($user['type'] == "IOS") {

                    \Log::info(":USERID=".$userId);
                    \Log::info("极光推送");
                    $res = $client->push()
                        ->setPlatform(array('ios'))
                        ->addRegistrationId([$user['registrationId']])
                        ->setNotificationAlert($title)
                        ->iosNotification($content, array(
                            'sound' => 'default',
                            'extras' => $extras
                        ))
                        ->message($content, array(
                            'title' => $title,
                            'content_type' => 'text',
                            'extras' => $extras,
                        ))->options(array(
                            //                    'apns_production' => false // true 生产环境 false 开发环境
                            'production' => true // true 生产环境 false 开发环境
                        ))

                        ->setOptions(100000, 86400, null, true)
                        ->send();
                    \Log::info($res);

                    return true;
                }
            }

        } else {
            \Log::info("极光推送");
            $pusher = $client->push();
            $res = $pusher->setPlatform(["android", "ios"])
                ->addAllAudience()
                ->setNotificationAlert($title)
                ->androidNotification($content, array(
                    'title' => $title,
                    'extras' => $extras
                ))
                ->iosNotification($content, array(
                    'sound' => 'default',
                    'extras' => $extras
                ))
                ->message($content, array(
                    'title' => $title,
                    'content_type' => 'text',
                    'extras' => $extras
                ))
                ->options(array(
                    //                    'apns_production' => false // true 生产环境 false 开发环境
                    'production' => true // true 生产环境 false 开发环境
                ))
                ->setOptions(100000, 86400, null, true)
                ->send();
            \Log::info($res);
            return true;
        }


    }
}